"use strict";

const GameManager = new function () {
	const that = {
		canvasList: {},
		diamondList: [],
		mission: 1,
		missionRange:[10000,30000,70000,110000,150000,190000,23000,27000,31000],
		missionTime: 3000,
		missionLazy: 2000,
		timeInterval: 50,
		timer: 0,
		row: 0,
		col: 0,
		diamondCanvas: null,
		nextDiamond: null,
		init: function () {
			that.initCanvas();
			that.initData();
			that.bindEvent();
		},
		initCanvas: function () {
			that.canvasList = {
				back: document.getElementById("back").getContext("2d"),
				diamond: document.getElementById("diamond").getContext("2d"),
				info: document.getElementById("info").getContext("2d")
			};
			that.diamondCanvas = document.getElementById("diamond");
		},
		initData: function () {
			PlaneInfoManager.init(that.canvasList.info);
			BackgroundHelper.draw(that.canvasList.back);
			//行 20
			that.row = BackgroundHelper.x;
			//列 12
			that.col = BackgroundHelper.y;
		},
		initDiamond: function () {

			const diamondType = that.random(1, DiamondData.length);

			//新增一个
			that.nextDiamond = new Diamond(that.canvasList.diamond, diamondType, 50, 0);

			that.diamondList.push(that.nextDiamond);
		},
		bindEvent: function () {
			document.onkeydown = function (e) {
				const event = window.event || e;
				switch (event.keyCode) {
					//Left
					case 37:
						console.log("keydown:left");
						that.nextDiamond.checkLeft();
						break;
					//Up
					case 38:
						console.log("keydown:up");
						that.nextDiamond.redenrDiamond();
						break;
					//Right
					case 39:
						console.log("keydown:right");
						that.nextDiamond.checkRight();
						break;
					//Down
					case 40:
						console.log("keydown:down");
						that.nextDiamond.checkDown();
						break;
				}
			};
		},
		random: function (min, max) {
			return Math.floor(Math.random() * (max - min)) + min;
		},
		start: function () {
			that.init();
			that.initDiamond();
			that.timer = setInterval(that.loop, this.timeInterval);
		},
		stop: function () {
			clearInterval(this.timer);
		},
		over: function () {
			this.stop();
			alert("你输了!");
		},
		loop: function () {
			console.log("game loop start...")
			Canvas.clear(that.canvasList.diamond, 300, 500);
			Canvas.clear(that.canvasList.back, 300, 500);
			BackgroundHelper.draw(that.canvasList.back);
			that.drawDiamond();
			that.updateDiamond();
			BackgroundHelper.checkIsFullRow();
			that.missionCheck();
		},
		missionCheck : function(){
			let speed = 0;
			for(let i = 0;i<this.missionRange.length;i++){
				let missionMaxVal = this.missionRange[i];
				if (PlaneInfoManager.score >= missionMaxVal){
					speed++;
				}
			}
			for (let i = 0;i<speed-PlaneInfoManager.speed;i++){
				PlaneInfoManager.updateSpeed();
				PlaneInfoManager.updateMission();
			}
			if (that.nextDiamond){
				that.nextDiamond.diamondTime = PlaneInfoManager.speedTime[speed];
			}
		},
		drawDiamond: function () {
			let diamond;

			let i = 0, l = that.diamondList.length;
			for (; i < l; i++) {

				diamond = that.diamondList[i];

				if (!diamond) continue;

				diamond.draw();
			}
		},
		updateDiamond: function () {
			let diamond;

			let i = 0, l = that.diamondList.length;
			for (; i < l; i++) {

				diamond = that.diamondList[i];

				if (!diamond) continue;

				diamond.update();
			}
		},
	};
	return that;
};